// 这是一个使用统一错误处理的示例页面
// 展示如何在实际页面中使用错误处理机制

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../../core/viewmodels/auth_viewmodel.dart';
import '../../../core/utils/error_handler_extension.dart';
import '../../../shared/widgets/error_widget.dart';

class LoginPageExample extends StatefulWidget {
  const LoginPageExample({super.key});

  @override
  State<LoginPageExample> createState() => _LoginPageExampleState();
}

class _LoginPageExampleState extends State<LoginPageExample> {
  final _usernameController = TextEditingController();
  final _passwordController = TextEditingController();

  @override
  void dispose() {
    _usernameController.dispose();
    _passwordController.dispose();
    super.dispose();
  }

  Future<void> _handleLogin() async {
    final viewModel = context.read<AuthViewModel>();
    
    final success = await viewModel.login(
      username: _usernameController.text,
      password: _passwordController.text,
    );

    if (!mounted) return;

    if (success) {
      // 登录成功，导航到首页
      Navigator.of(context).pushReplacementNamed('/home');
    } else {
      // 使用扩展方法显示错误
      context.showError(viewModel.errorMessage);
      
      // 或者使用 Dialog
      // await context.showErrorDialog(
      //   viewModel.errorMessage,
      //   title: '登录失败',
      // );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('登录示例')),
      body: Consumer<AuthViewModel>(
        builder: (context, viewModel, child) {
          // 如果 ViewModel 有错误，可以显示错误组件
          if (viewModel.errorMessage != null && !viewModel.isLoading) {
            return AppErrorWidget(
              error: viewModel.errorMessage!,
              onRetry: () {
                viewModel.clearError();
                _handleLogin();
              },
            );
          }

          return Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                TextField(
                  controller: _usernameController,
                  decoration: const InputDecoration(
                    labelText: '用户名',
                    border: OutlineInputBorder(),
                  ),
                ),
                const SizedBox(height: 16),
                TextField(
                  controller: _passwordController,
                  obscureText: true,
                  decoration: const InputDecoration(
                    labelText: '密码',
                    border: OutlineInputBorder(),
                  ),
                ),
                const SizedBox(height: 24),
                ElevatedButton(
                  onPressed: viewModel.isLoading ? null : _handleLogin,
                  child: viewModel.isLoading
                      ? const CircularProgressIndicator()
                      : const Text('登录'),
                ),
              ],
            ),
          );
        },
      ),
    );
  }
}

